package cn.iocoder.springboot.lab04.rabbitmqdemo.config;

import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo01Message;
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo02Message;
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo03Message;
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo04Message;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public static class DirectExchangeDemoConfiguration {
        @Bean
        public Queue demo01Queue() {
            return new Queue(Demo01Message.QUEUE, true, false, false);
        }

        @Bean
        public DirectExchange demo01Exchange() {
            return new DirectExchange(Demo01Message.EXCHANGE, true, false);
        }

        @Bean
        public Binding demo01Binding() {
            return BindingBuilder.bind(demo01Queue()).to(demo01Exchange()).with(Demo01Message.ROUTING_KEY);
        }
    }

    public static class TopicExchangeDemoConfiguration {
        @Bean
        public Queue demo02Queue() {
            return new Queue(Demo02Message.QUEUE, true, false, false);
        }

        @Bean
        public TopicExchange demo02Exchange() {
            return new TopicExchange(Demo02Message.EXCHANGE, true, false);
        }

        @Bean
        public Binding demo02Binding() {
            return BindingBuilder.bind(demo02Queue()).to(demo02Exchange()).with(Demo02Message.ROUTING_KEY);
        }
    }

    public static class FanoutExchangeDemoConfiguration {
        @Bean
        public Queue demo03QueueA() {
            return new Queue(Demo03Message.QUEUE_A, true, false, false);
        }

        @Bean
        public Queue demo03QueueB() {
            return new Queue(Demo03Message.QUEUE_B, true, false, false);
        }

        @Bean
        public FanoutExchange demo03Exchange() {
            return new FanoutExchange(Demo03Message.EXCHANGE, true, false);
        }

        @Bean
        public Binding demo03BindingA() {
            return BindingBuilder.bind(demo03QueueA()).to(demo03Exchange());
        }

        @Bean
        public Binding demo03BindingB() {
            return BindingBuilder.bind(demo03QueueB()).to(demo03Exchange());
        }
    }

    public static class HeadersExchangeDemoConfiguration {
        @Bean
        public Queue demo04Queue() {
            return new Queue(Demo04Message.QUEUE, true, false, false);
        }

        @Bean
        public HeadersExchange demo04Exchange() {
            return new HeadersExchange(Demo04Message.EXCHANGE, true, false);
        }

        @Bean
        public Binding demo04Binding() {
            return BindingBuilder.bind(demo04Queue()).to(demo04Exchange())
                    .where(Demo04Message.HEADER_KEY).matches(Demo04Message.HEADER_VALUE);
        }
    }
}
